<template>
  <div id="category">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item><a href="/home">首页</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-row>
        <vxe-table
          :data="categoryList"
          border
          ref="xTree"
          :tree-config="{ children: 'children' }"
        >
          <vxe-table-column type="seq" title="#" width="60"></vxe-table-column>
          <vxe-table-column
            field="cat_name"
            title="分类名称"
            tree-node
          ></vxe-table-column>
          <vxe-table-column title="是否有效">
            <template v-slot:default="scope">
              <i
                class="el-icon-success"
                v-if="scope.row.cat_deleted === false"
                style="color: lightgreen"
              ></i>
              <i
                class="el-icon-error"
                v-if="scope.row.cat_deleted !== false"
                style="color: red"
              ></i>
            </template>
          </vxe-table-column>
          <vxe-table-column title="排序">
            <template v-slot:default="scope">
              <el-tag type="primary" v-if="scope.row.cat_level === 0"
                >一级</el-tag
              >
              <el-tag type="success" v-if="scope.row.cat_level === 1"
                >二级</el-tag
              >
              <el-tag type="warning" v-if="scope.row.cat_level === 2"
                >三级</el-tag
              >
            </template>
          </vxe-table-column>
          <vxe-table-column title="操作">
            <template v-slot:default="scope">
              {{ scope.row.id }}
              <el-button type="primary" size="small">编辑</el-button>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </vxe-table-column>
        </vxe-table>
      </el-row>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      v-model="addCateVisible"
      width="50%"
    >
      <span>这是一段信息</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addCateVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCateVisible = false"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'category',
  data() {
    return {
      categoryList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 分类数据总条数
      total: 0,
      // 是否展示添加分类对话框
      addCateVisible: false,
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    async getCategoryList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('初始化商品分类数据失败！')
      }
      // 把数据列表，赋值给 categoryList
      this.categoryList = res.data.result
      // 为总数据条数赋值
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCategoryList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getCategoryList()
    },
  },
}
</script>

<style lang="less" scoped>
.el-button {
  margin: 10px 5px;
}
</style>
